//
//  UGC_Release_Tbv_WX.m
//  ZITHER
//
//  Created by weixin on 2018/9/26.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "UGC_Release_Tbv_WX.h"
#import "Ugc_Receive_TbvCell_WX.h"
#import "Ugc_Receive_SystemCell_WX.h"

#import "UgcReceiveModel.h"
#import "Ugc_Detail_VC_WX.h"
#import "MainFirst_Magazine_VC_WX.h"

@implementation UGC_Release_Tbv_WX

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self defaultConfiguration];
        [self.mj_header beginRefreshing];
    }
    return self;
}


-(void)setLoad{
    @weakify(self);
 
    [[serviceDefualt loadGetUserMsgListPage:self.page reponseObjctClass:[UgcReceiveModel class]]subscribeNext:^(ResponseData * x) {
        @strongify(self);
        UgcReceiveModel *model = x.responseData;
        NSArray *arr = model.list;
        if (x.success) {
            
            [self setNoMoreDataBg:YES NoDataState:NoMoreDataStateDefault];
            if (arr.count == 0) {
                if (self.page == 0) {
//                    [Tools showTips:@"暂无更多数据"];
                    [self setNoMoreDataBg:NO NoDataState:NoMoreDataStateDefault];
                    self.noMoreDateView.title.text = @"没有消息";
                    [self.mj_header endRefreshing];
                    [self.mj_footer endRefreshing];
                    [self.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.mj_footer endRefreshingWithNoMoreData];
                }
                return ;
            }
            
            if (self.page == 0) {
                self.arrM = [[NSMutableArray alloc]init];
                if (arr.count == 0 ) {
                    [Tools showTips:@"暂无更多数据"];
                    [self reloadData];
                }else{
                    for (UgcReceiveModelSon *model in arr) {
                        [self.arrM addObject: model];
                    }
                    [self reloadData];
                }
            }else{
                if (arr.count == 0 ) {
                    [Tools showTips:@"暂无更多数据"];
                }else{
                    for (UgcReceiveModelSon *model in arr) {
                        [self.arrM addObject: model];
                    }
                    [self reloadData];
                }
            }
            
        }else{
            [Tools showTips:x.errorInfo];
        }
        [self.mj_header endRefreshing];
        [self.mj_footer endRefreshing];
    }];
}



-(void)defaultConfiguration{
    self.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 20)];
    self.delegate = self;
    self.dataSource = self;
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.rowHeight = UITableViewAutomaticDimension;
    self.estimatedRowHeight = 1000;
    [self registerNib:[UINib nibWithNibName:@"Ugc_Receive_TbvCell_WX" bundle:nil] forCellReuseIdentifier:@"Ugc_Receive_TbvCell_WX"];
        [self registerNib:[UINib nibWithNibName:@"Ugc_Receive_SystemCell_WX" bundle:nil] forCellReuseIdentifier:@"Ugc_Receive_SystemCell_WX"];
    @weakify(self);
       self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
         @strongify(self);
        self.page ++;
        [self setLoad];
    }];
    self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        self.page = 0;
        [self setLoad];
    }];
    
}


#pragma MARK - 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrM.count;
}


/**
    1.点赞 UGC 有图
    2.点赞 UGC 无图
    3.点赞 UGC 已删除
    4.被别人子评论 UGC 有图
    5.被别人子评论 UGC 无图
    6.被别人子评论 UGC 已删除
    7.自己发布 UGC 被别人主评论 UGC 有图
    8.自己发布 UGC 被别人主评论 UGC 无图
    9.自己发布 UGC 被别人主评论 UGC 已删除
    10.系统消息
 */


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UgcReceiveModelSon *model = self.arrM[indexPath.row];
    
    if ([model.msgType isEqualToString:@"0"]) {
        
        static NSString *sectionCell = @"Ugc_Receive_SystemCell_WX";
        Ugc_Receive_SystemCell_WX *cell = [tableView dequeueReusableCellWithIdentifier:sectionCell];
        if (cell == nil) {
            cell = [[Ugc_Receive_SystemCell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
        }
        
        cell.typeMsgStatus = [self changeTypeMsgStatus:indexPath.row];
        
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;
        cell.modelSon = model;
        
        return cell;
    }else{
        static NSString *sectionCell = @"Ugc_Receive_TbvCell_WX";
        Ugc_Receive_TbvCell_WX *cell = [tableView dequeueReusableCellWithIdentifier:sectionCell];
        if (cell == nil) {
            cell = [[Ugc_Receive_TbvCell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
        }
        
        cell.typeMsgStatus = [self changeTypeMsgStatus:indexPath.row];
        
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;

        cell.modelSon = model;
        
        return cell;
    }
    
    
}


-(NSString *)changeTypeMsgStatus:(NSInteger)index{
    UgcReceiveModelSon *model = self.arrM[index];

    if (index == 0) {
        if ([model.msgStatus isEqualToString:@"0"]) {
            self.typeMsgStatusNew = @"1";
            return @"收到的消息";
        }else{
            self.typeMsgStatus = @"1";
            return @"更早的消息";
        }
    }else{
        if ([model.msgStatus isEqualToString:@"1"]) {
            if (![self.typeMsgStatus isEqualToString:@"1"]) {
                self.typeMsgStatus = @"1";
                return @"更早的消息";
            }
        }
    }
    return @"";
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"section == %ld,row == %ld",indexPath.section,indexPath.row);

    UgcReceiveModelSon *model = self.arrM[indexPath.row];
    /** 关联消息主体类型（1：ugc 2:pgc）*/
    
    if (![model.msgType isEqualToString:@"4"] && ![model.msgType isEqualToString:@"0"]) {
        if (![model.delStatus isEqualToString:@"0"]) {
            if ([model.relType isEqualToString:@"1"]) {
                Ugc_Detail_VC_WX *vc = [[Ugc_Detail_VC_WX alloc]init];
                vc.ugcId = model.relId;
                vc.userID = model.userId;
                [self.getNav pushViewController:vc animated:YES];
            }else{
                MainFirst_Magazine_VC_WX *vc = [[MainFirst_Magazine_VC_WX alloc]init];
                vc.pgcId = model.relId;
                [self.getNav pushViewController:vc animated:YES];
            }
        }else{
            [Tools showTips:@"相关的创作已被删除"];
        }
    }else{
        [Tools showTips:@"相关的创作已被系统删除"];
    }
   
}

-(NSMutableArray *)arrM{
    if (!_arrM) {
        _arrM = [[NSMutableArray alloc]init];
    }
    return _arrM;
}

@end
